package cn.edu.scau.cmi.huangzhiyuan.db.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import cn.edu.scau.cmi.huangzhiyuan.db.domain.Student;
import cn.edu.scau.cmi.huangzhiyuan.db.domain.Teacher;

public class TeacherDao extends HibernateBaseDao {

	public TeacherDao() {
	}
	
	public void save(Teacher teacher) {
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.save(teacher);
			tx.commit();
		} catch (Exception e) {
			System.out.println("TeacherDao save func has an error: " + e);
		}
	}
	
	
	public void update(Teacher teacher) {
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.saveOrUpdate(teacher);
			tx.commit();
		} catch (Exception e) {
			System.out.println("TeacherDao update func has an error: " + e);
		}
	}
	
	public Teacher findById(int id) {
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			Teacher teacher = session.get(Teacher.class, id);
			tx.commit();
			return teacher;
		} catch (Exception e) {
			System.out.println("TeacherDao update func has an error:" + e);
			throw e;
		}
	}
	
	public List findByName(Object property) {
		try {
			String hql = "from Teacher as model where model.name=" + property;
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			List teachers = session.createQuery(hql).list();
			tx.commit();
			return teachers;
		} catch (Exception e) {
			System.out.println("TeacherDao findByProperty has an error:" + e);
			throw e;
		}
	}
	
	
	public void delete(Teacher teacher) {
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.delete(teacher);
			tx.commit();
		} catch (Exception e) {
			System.out.println("TeacherDao delete func has an error:" + e);
		}
	}

	
	public List findAll() {
		try {
			String hql = "from Teacher";
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			Query query = session.createQuery(hql);
			List teachers = query.list();
			tx.commit();
			return teachers;
		} catch (Exception e) {
			System.out.println("TeacherDao findAll has an error:" + e);
			throw e;
		}
	}


}
